package io.renren.modules.sys.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.JxCourseTypeDao;
import io.renren.modules.sys.entity.JxCourseTypeEntity;
import io.renren.modules.sys.service.JxCourseTypeService;


@Service("jxCourseTypeService")
public class JxCourseTypeServiceImpl extends ServiceImpl<JxCourseTypeDao, JxCourseTypeEntity> implements JxCourseTypeService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<JxCourseTypeEntity> page = this.page(
                new Query<JxCourseTypeEntity>().getPage(params),
                new QueryWrapper<JxCourseTypeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    //@DataFilter(subCourse = true, user = false, tableAlias = "t1")
    public List<JxCourseTypeEntity> queryList(Map<String, Object> params) {
        return baseMapper.queryList(params);
    }

    @Override
    public List<Long> queryJxCourseIdList(Long parentId) {
        return baseMapper.queryIdList(parentId);
    }

    @Override
    public List<Long> getSubJxCourseIdList(Long id) {
        //部门及子部门ID列表
        List<Long> idList = new ArrayList<>();

        //获取子部门ID
        List<Long> subIdList = queryJxCourseIdList(id);
        getJxCourseTreeList(subIdList, idList);

        return idList;
    }

    /**
     * 递归
     */
    private void getJxCourseTreeList(List<Long> subIdList, List<Long> idList){
        for(Long id : subIdList){
            List<Long> list = queryJxCourseIdList(id);
            if(list.size() > 0){
                getJxCourseTreeList(list, idList);
            }
            idList.add(id);
        }
    }
}
